package lk.icta.core.service.impl;

import lk.icta.common.ELandRuntimeException;
import lk.icta.common.ErrorCodes;
import lk.icta.core.dao.LocalAuthorityDAO;
import lk.icta.core.entity.LocalAuthority;
import lk.icta.core.service.LocalAuthorityManagementService;
import lk.icta.user.common.Permission;
import lk.icta.user.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Kusal Samarakoon
 */

public class LocalAuthorityManagementServiceImpl implements LocalAuthorityManagementService {

    private static final Logger logger = LoggerFactory.getLogger(LocalAuthorityManagementServiceImpl.class);

    private final LocalAuthorityDAO localAuthorityDAO;

    public LocalAuthorityManagementServiceImpl(LocalAuthorityDAO localAuthorityDAO) {
        this.localAuthorityDAO = localAuthorityDAO;

    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addLocalAuthority(LocalAuthority localAuthority, User user) {
        logger.debug("Adding a addLocalAuthority {}", user.getUsername());
        localAuthorityDAO.addLocalAuthority(localAuthority, user);
        /*if (!(user.isAuthorized(Permission.MANAGE_INITIAL_LAND_APPLICATION) ||
                user.isAuthorized(Permission.MANAGE_GOVERNMENT_ORGANIZATION))) {
            throw new ELandRuntimeException(user.getUsername() +
                    " doesn't have permission to add a addLocalAuthority", ErrorCodes.UNAUTHORIZED_ACCESS);
        } else {
            localAuthorityDAO.addLocalAuthority(localAuthority,user);
        }*/
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateLocalAuthority(LocalAuthority localAuthority, User user) {
        logger.debug("Adding a updateLocalAuthority {}", user.getUsername());
        localAuthorityDAO.updateLocalAuthority(localAuthority, user);
        /*if (!(user.isAuthorized(Permission.MANAGE_INITIAL_LAND_APPLICATION) ||
                user.isAuthorized(Permission.MANAGE_GOVERNMENT_ORGANIZATION))) {
            throw new ELandRuntimeException(user.getUsername() +
                    " doesn't have permission to add a addLocalAuthority", ErrorCodes.UNAUTHORIZED_ACCESS);
        } else {
            localAuthorityDAO.addLocalAuthority(localAuthority,user);
        }*/
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void inActiveLocalAuthority(int localAuthorityId, User user) {
        logger.debug("Adding a inActiveLocalAuthority {}", user.getUsername());
        LocalAuthority localAuthority=localAuthorityDAO.getLocalAuthorityById(localAuthorityId,user);
        localAuthorityDAO.inActiveLocalAuthority(localAuthority,user);
        /*if (!(user.isAuthorized(Permission.MANAGE_INITIAL_LAND_APPLICATION) ||
                user.isAuthorized(Permission.MANAGE_GOVERNMENT_ORGANIZATION))) {
            throw new ELandRuntimeException(user.getUsername() +
                    " doesn't have permission to add a addLocalAuthority", ErrorCodes.UNAUTHORIZED_ACCESS);
        } else {
            localAuthorityDAO.addLocalAuthority(localAuthority,user);
        }*/
    }


}
